package com.milasoft.queueingcloud;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.parse.*;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class UserPageActivity extends ActionBarActivity {
	static int queueCount = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.user_page);
		final TextView outputPlaceName = (TextView) findViewById(R.id.cloudselect);
		final TextView tvUserValue = (TextView) findViewById(R.id.tvUserValue);
		final TextView tvPlaceValue = (TextView) findViewById(R.id.tvPlaceValue);
		final TextView tvTimeValue = (TextView) findViewById(R.id.tvTimeValue);
		final TextView tvQueueValue = (TextView) findViewById(R.id.tvQueueValue);
		final EditText userName = (EditText) findViewById(R.id.edUserName);

		new CountDownTimer(5000, 1000) {

			public void onTick(long millisUntilFinished) {

			}

			public void onFinish() {
				start();
				ParseQuery<ParseObject> query = ParseQuery
						.getQuery("CustomersQueue");
				query.findInBackground(new FindCallback<ParseObject>() {
					public void done(List<ParseObject> scoreList,
							ParseException e) {
						if (e == null) {
							for (int i = 0; i < scoreList.size(); i++) {

								queueCount = scoreList.get(i).getInt("QueueNo");
							}

						} else {
							Log.d("score", "Error: " + e.getMessage());
						}
					}
				});
			}
		}.start();

		Button button = (Button) findViewById(R.id.takeQueue);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
				String currentDateandTime1 = sdf1.format(new Date());

				ParseObject testObject = new ParseObject("CustomersQueue");
				testObject.put("UserName", userName.getText().toString());
				testObject.put("PlaceName", "Is Bankasi");// Cloud Data Baseden
															// cekecegim
				testObject.put("EntryTime", currentDateandTime1);
				testObject.put("QueueNo", queueCount + 1);
				testObject.saveInBackground();

			}

		});
		Button buttonShow = (Button) findViewById(R.id.showdata);
		buttonShow.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				ParseQuery<ParseObject> query = ParseQuery
						.getQuery("CustomersQueue");
				query.findInBackground(new FindCallback<ParseObject>() {
					public void done(List<ParseObject> scoreList,
							ParseException e) {
						if (e == null) {
							for (int i = 0; i < scoreList.size(); i++) {
								tvUserValue.setText(scoreList.get(i).getString(
										"UserName"));
								tvPlaceValue.setText(scoreList.get(i)
										.getString("PlaceName"));
								tvTimeValue.setText(scoreList.get(i).getString(
										"EntryTime"));
								tvQueueValue.setText(scoreList.get(i)
										.getNumber("QueueNo").toString());
							}

						} else {
							Log.d("score", "Error: " + e.getMessage());
						}
					}
				});

			}

		});
		Button buttondelete = (Button) findViewById(R.id.deleteButton);
		buttondelete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				ParseQuery<ParseObject> query = ParseQuery
						.getQuery("CustomersQueue");
				query.findInBackground(new FindCallback<ParseObject>() {
					public void done(List<ParseObject> scoreList,
							ParseException e) {
						if (e == null) {

							int i=0;
							int queueno=0;
							for(ParseObject invite : scoreList){
								if(i==0){
									invite.deleteInBackground();
									
									
								}
								queueno=scoreList.get(i).getInt("QueueNo")-1;
								invite.put("QueueNo",queueno);
							    
								invite.saveInBackground();
								i++;
								 }
							

						} else {
							Log.d("score", "Error: " + e.getMessage());
						}
					}
				});
				
					}
					
		

			

		});
	}

}
